//
//  XAspect-AppDelegateNotificationConfig 抽离原本应在AppDelegate的内容
//  YTT-IOS-CommonFramework
//
//  Created by aron on 2017/2/22.
//  Copyright © 2017年 aron. All rights reserved.
//

#import "AppDelegate.h"
#import "XAspect.h"
#import "MMFCMManager.h"
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif


#define AtAspect NotificationConfig

#define AtAspectOfClass AppDelegate
@classPatchField(AppDelegate)
AspectPatch(-, BOOL, application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions)
{
    // FCM 配置
    [[MMFCMManager sharedInstance] setupNotificationConfigWithApplication:application];

    return XAMessageForward(application:application didFinishLaunchingWithOptions:launchOptions);
}

AspectPatch(-, void, applicationDidEnterBackground:(UIApplication *)application)
{
    [[MMFCMManager sharedInstance] disconnect];
    
    XAMessageForwardDirectly(applicationDidEnterBackground:application);
}


AspectPatch(-, void, applicationDidBecomeActive:(UIApplication *)application)
{
    // FCM配置
    [[MMFCMManager sharedInstance] connectToFcm];
    [MMFCMManager sharedInstance].isEnterFromBackground = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MMFCMManager sharedInstance].isEnterFromBackground = NO;
    });
    BMLog(@"====applicationDidBecomeActive");
    
    XAMessageForwardDirectly(applicationDidBecomeActive:application);
}

// [START receive_message]
AspectPatch(-, void, application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo)
{
    [self handleNotification:userInfo];
    XAMessageForwardDirectly(application:application didReceiveRemoteNotification:userInfo);
}

AspectPatch(-, void, application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler)
{
    [self handleNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    XAMessageForwardDirectly(application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler);
}
// [END receive_message]

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// [START ios_10_message_handling]
AspectPatch(-, void, userNotificationCenter:(UNUserNotificationCenter *)center
            willPresentNotification:(UNNotification *)notification
            withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler)
{
    NSDictionary *userInfo = notification.request.content.userInfo;
    [self handleNotification:userInfo];
    completionHandler(UNNotificationPresentationOptionNone);
    XAMessageForwardDirectly(userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler);
}

AspectPatch(-, void, userNotificationCenter:(UNUserNotificationCenter *)center
            didReceiveNotificationResponse:(UNNotificationResponse *)response
            withCompletionHandler:(void (^)())completionHandler)
{
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    [self handleNotification:userInfo];
    completionHandler();
    XAMessageForwardDirectly(userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler: completionHandler);
}
// [END ios_10_message_handling]
#endif


AspectPatch(-, void, application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error)
{
    NSLog(@"Unable to register for remote notifications: %@", error);

    XAMessageForwardDirectly(application:application didFailToRegisterForRemoteNotificationsWithError:error);
}

// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the InstanceID token.
AspectPatch(-, void, application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken)
{
    NSLog(@"APNs token retrieved: %@", deviceToken);
    // With swizzling disabled you must set the APNs token here.
    // [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
    
    XAMessageForwardDirectly(application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken);
}


/**
 处理通知,在MMFCMManager管理类中统一处理
 @param userInfo 通知信息
 */
- (void)handleNotification:(NSDictionary *)userInfo {
    [[MMFCMManager sharedInstance] handleNotification:userInfo];
}

@end
#undef AtAspectOfClass
#undef AtAspect
